define(function(require, exports, module) {
  var $ = require('$');
  var formcc = require('form');
  var nav = require("bootstrap_nav");
  var bootbox = require("bootbox");
  var store = require("store");
  var loader = require("./loader/loader");
  var dtUtil = require('datatableutil');
  require("bootstrap-datetimepicker.min.css");
  //require("bootstrap_datetimepicker.zh-CN");
  require("bootstrap_datetimepicker");
  require('select2.css');
  require('select2');

  exports.init = function(divId, data) {
    var isvrecord = {};
    if (data)
      isvrecord = data;
    
    
    
//加载表单模板和数据
  var Table = require("table");
  // 查询内容显示
  new Table(
          {
              attrs :
              {
                  id : "isvinfo_data_table",
                  isHovered : false,
                  isStriped : true,
                  isBordered : false
              },
              renderTo : divId
             
          });
  // menu信息显示
  var isvinfo_data_table = $('#isvinfo_data_table').dataTable({
          "bJQueryUI" : false,
          "oLanguage" :
          {
              "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
          },
          "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
          'bPaginate' : false, // 是否分页
          "bProcessing" : false, // 当datatable获取数据时候是否显示正在处理提示信息。
          "bServerSide" : false, // 指定服务器端分页
          "bDeferRender" : false,
          "bFilter" : false,
          "bSort" : false,
          "bInfo" : false,
          "sAjaxSource" : "/role/queryUserByRole.do?roleid="+isvrecord.id, 
          "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.DEFAULT,  // 默认分页的每页记录数
          "fnServerData" : function(sSource, aoData, fnCallback, oSettings)
          {
              
             // 添加自己的额外参数
             oSettings.jqXHR = $.ajax(
             {
                "dataType" : 'json',
                 "contentType" : "application/json;charset=UTF-8",
                 "type" : "GET",
                 "url" : sSource,
                 "data" : null, // 将请求数据转换为后台要求的JSON格式
                 "success" : function(json)
                 {
                   if(json.flag == '000000'){
                     var idsArray = new Array();
                     var dataArray = json.data.items;
                     for(var i=0;i<dataArray.length;i++){
                       var dataObj = dataArray[i];
                       if(dataObj.check_status != -1){
                         idsArray.push(dataObj.id);                       
                       }
                     }
                     $("body").data("dataArray", idsArray);
                     dtUtil.successCallback(json, aoData, fnCallback);                    
                   } else {
                     oauth2Relogin();                    
                   }
                 }
            });
              
              
          },
          "aoColumns" : [
      {
        "sTitle" : "id",
        "bSortable" : false,
        "mData" : "id",
        "bVisible":false
      },        
      {
        "sTitle" : '<input class="only-checkbox" type="checkbox" id="checkall">',
        "bSortable" : false,
        "sWidth" : "5%",
        "mData" : "check_status",
        "mRender" : function(data, type, full)
        {
          if (data == -1)
            return '<input class="only-checkbox tableCheckbox" type="checkbox">';
          else{
            return '<input class="only-checkbox tableCheckbox" type="checkbox" checked>';
          }
        }
      },
      {
        "sTitle" : "名称",
        "bSortable" : false,
        "sWidth" : "20%",
        "mData" : "name"
      },
      {
        "sTitle" : "账号",
        "bSortable" : false,
        "sWidth" : "10%",
        "mData" : "account"
      },
      {
        "sTitle" : "类型",
        "bSortable" : false,
        "sWidth" : "10%",
        "mData" : "type"
      },
      {
        "sTitle" : "创建时间",
        "bSortable" : false,
        "sWidth" : "15%",
        "mData" : "create_time"
      }],
        "fnDrawCallback": function( oSettings ) {
            // 表格绘制完成后，去除width属性，使表格可缩放
            $('#'+oSettings.sTableId).css("width", "");
          }
    });
  
  //全选全消
  $("#isvinfo_data_table thead").on("click", "#checkall", function(e)
  {
    var check = $(this).attr("checked");
    var checkboxs = $(".tableCheckbox");
    var dataArray = $("body").data("dataArray");
    if (!dataArray)
      dataArray = new Array();
    for (var i = 0; i < checkboxs.length; i++)
    {
      var checkbox = checkboxs[i];
      var rowData = isvinfo_data_table.fnGetData(i);
      if (check == "checked")
      {
          $(checkbox).attr("checked", 'true');
          if(dataArray.indexOf(rowData.id) == -1){
            dataArray.push(rowData.id);
          }
      }
      else
      {
          $(checkbox).removeAttr("checked");
          dataArray.splice($.inArray(rowData.id, dataArray), 1);
      }
    }
    $("body").data("dataArray", dataArray);
  });
  //复选框
  $("#isvinfo_data_table tbody").on("click", " tr td .tableCheckbox", function()
  {
    var rowNode = this.parentNode.parentNode;
    var rowData = isvinfo_data_table.fnGetData(rowNode);
    var check = $(this).attr("checked");
    var dataArray = $("body").data("dataArray");
    if (!dataArray)
      dataArray = new Array();
    if (check)
      dataArray.push(rowData.id);
    else
      dataArray.splice($.inArray(rowData.id, dataArray), 1);

    $("body").data("dataArray", dataArray);
  });
  
  //保存
  $("#roleUserbtn").on("click", function()
  {
    var dataArray = $("body").data("dataArray");
    loader.loading("正在保存，请稍等……");
    $.ajax({
      url : "/role/updateRoleUser.do",//GLOBAL.URL.ADMIN_INSERTLICENSECARRIER,
      dataType : "json",
      data : "data=" + dataArray+"&roleid="+isvrecord.id,
      type : "POST",
      success : function(json) {
        if(json.flag == '000000'){
          nav.toPageView("admin/role.html");
          loader.noloading();                   
        } else {
          oauth2Relogin();                    
        }
      },
      error : function(s) {
        loader.noloading();
        bootbox.alertTimeout("网络请求失败，请重试！");
      }
    });
  });
  
  //返回
  $("#backbtn").on("click", function()
  {
    nav.toPageView("admin/role.html");
    loader.noloading();
  });
  
  
  };
  
});
